package com.marketingcenter.controller;

import com.marketingcenter.entity.ThresholdPromotion;
import com.marketingcenter.service.ThresholdPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 满减满赠满返活动控制器
 */
@RestController
@RequestMapping("/threshold-promotions")
public class ThresholdPromotionController {

    @Autowired
    private ThresholdPromotionService thresholdPromotionService;

    /**
     * 创建满减满赠满返活动
     */
    @PostMapping
    public ResponseEntity<ThresholdPromotion> create(@RequestBody ThresholdPromotion promotion) {
        promotion.setType(ThresholdPromotion.PromotionType.THRESHOLD);
        ThresholdPromotion createdPromotion = thresholdPromotionService.create(promotion);
        return ResponseEntity.ok(createdPromotion);
    }

    /**
     * 更新满减满赠满返活动
     */
    @PutMapping("/{id}")
    public ResponseEntity<ThresholdPromotion> update(@PathVariable Long id, @RequestBody ThresholdPromotion promotion) {
        promotion.setId(id);
        ThresholdPromotion updatedPromotion = thresholdPromotionService.update(promotion);
        return ResponseEntity.ok(updatedPromotion);
    }

    /**
     * 删除满减满赠满返活动
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        thresholdPromotionService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID查询满减满赠满返活动
     */
    @GetMapping("/{id}")
    public ResponseEntity<ThresholdPromotion> getById(@PathVariable Long id) {
        ThresholdPromotion promotion = thresholdPromotionService.findById(id);
        return promotion != null ? ResponseEntity.ok(promotion) : ResponseEntity.notFound().build();
    }

    /**
     * 查询所有满减满赠满返活动
     */
    @GetMapping
    public ResponseEntity<List<ThresholdPromotion>> getAll() {
        List<ThresholdPromotion> promotions = thresholdPromotionService.findAll();
        return ResponseEntity.ok(promotions);
    }

    /**
     * 根据状态查询满减满赠满返活动
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<ThresholdPromotion>> getByStatus(@PathVariable Integer status) {
        List<ThresholdPromotion> promotions = thresholdPromotionService.findByStatus(status);
        return ResponseEntity.ok(promotions);
    }

    /**
     * 查询活动中的满减满赠满返活动
     */
    @GetMapping("/active")
    public ResponseEntity<List<ThresholdPromotion>> getActive() {
        List<ThresholdPromotion> promotions = thresholdPromotionService.findActivePromotions();
        return ResponseEntity.ok(promotions);
    }

    /**
     * 启用满减满赠满返活动
     */
    @PutMapping("/{id}/enable")
    public ResponseEntity<Void> enable(@PathVariable Long id) {
        thresholdPromotionService.enable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 停用满减满赠满返活动
     */
    @PutMapping("/{id}/disable")
    public ResponseEntity<Void> disable(@PathVariable Long id) {
        thresholdPromotionService.disable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 计算订单可享受的优惠金额
     */
    @GetMapping("/{id}/calculate-discount")
    public ResponseEntity<Double> calculateDiscount(@PathVariable Long id, @RequestParam double orderAmount) {
        double discount = thresholdPromotionService.calculateDiscount(id, orderAmount);
        return ResponseEntity.ok(discount);
    }

    /**
     * 检查订单是否满足活动条件
     */
    @GetMapping("/{id}/check-eligibility")
    public ResponseEntity<Boolean> checkEligibility(@PathVariable Long id, 
                                                   @RequestParam double orderAmount, 
                                                   @RequestParam(required = false) String itemIds) {
        boolean eligible = thresholdPromotionService.checkOrderEligibility(id, orderAmount, itemIds);
        return ResponseEntity.ok(eligible);
    }

}